home *** CD-ROM | disk | FTP | other *** search
/ Total Network Tools 2002 / NextStepPublishing-TotalNetworkTools2002-Win95.iso / Archive / Misc Servers / Zope.exe / MULTIMAPPING.STX < prev    next >
Encoding:
Text File  |  1999-03-23  |  13.4 KB  |  409 lines

  1. Example: MultiMapping objects
  2.  
  3.   "Copyright (C) 1996-1998, Digital Creations":COPYRIGHT.html.
  4.  
  5.   As an example, consider an extension class that implements a
  6.   "MultiMapping". A multi-mapping is an object that encapsulates 0
  7.   or more mapping objects.  When an attempt is made to lookup an
  8.   object, the encapsulated mapping objects are searched until an
  9.   object is found.
  10.  
  11.   Consider an implementation of a MultiMapping extension type,
  12.   without use of the extension class mechanism::
  13.  
  14.     #include "Python.h"
  15.     
  16.     #define UNLESS(E) if(!(E))
  17.     
  18.     typedef struct {
  19.         PyObject_HEAD
  20.         PyObject *data;
  21.     } MMobject;
  22.     
  23.     staticforward PyTypeObject MMtype;
  24.     
  25.     static PyObject *
  26.     MM_push(MMobject *self, PyObject *args){
  27.         PyObject *src;
  28.         UNLESS(PyArg_ParseTuple(args, "O", &src)) return NULL;
  29.         UNLESS(-1 != PyList_Append(self->data,src)) return NULL;
  30.         Py_INCREF(Py_None);
  31.         return Py_None;
  32.     }
  33.     
  34.     static PyObject *
  35.     MM_pop(MMobject *self, PyObject *args){
  36.         long l;
  37.         PyObject *r;
  38.         static PyObject *emptyList=0;
  39.     
  40.         UNLESS(emptyList) UNLESS(emptyList=PyList_New(0)) return NULL;
  41.         UNLESS(PyArg_ParseTuple(args, "")) return NULL;
  42.         UNLESS(-1 != (l=PyList_Size(self->data))) return NULL;
  43.         l--;
  44.         UNLESS(r=PySequence_GetItem(self->data,l)) return NULL;
  45.         UNLESS(-1 != PyList_SetSlice(self->data,l,l+1,emptyList)) goto err;
  46.         return r;
  47.     err:
  48.         Py_DECREF(r);
  49.         return NULL;
  50.     }
  51.     
  52.     static struct PyMethodDef MM_methods[] = {
  53.         {"push", (PyCFunction) MM_push, 1,
  54.          "push(mapping_object) -- Add a data source"},
  55.         {"pop",  (PyCFunction) MM_pop,  1,
  56.          "pop() -- Remove and return the last data source added"}, 
  57.         {NULL,              NULL}           /* sentinel */
  58.     };
  59.     
  60.     static PyObject *
  61.     newMMobject(PyObject *ignored, PyObject *args){
  62.         MMobject *self;
  63.               
  64.         UNLESS(PyArg_ParseTuple(args, "")) return NULL;
  65.         UNLESS(self = PyObject_NEW(MMobject, &MMtype)) return NULL;
  66.         UNLESS(self->data=PyList_New(0)) goto err;
  67.         return (PyObject *)self;
  68.     err:
  69.         Py_DECREF(self);
  70.         return NULL;
  71.     }
  72.     
  73.     static void
  74.     MM_dealloc(MMobject *self){
  75.         Py_XDECREF(self->data);
  76.         PyMem_DEL(self);
  77.     }
  78.     
  79.     static PyObject *
  80.     MM_getattr(MMobject *self, char *name){
  81.         return Py_FindMethod(MM_methods, (PyObject *)self, name);
  82.     }
  83.     
  84.     static int
  85.     MM_length(MMobject *self){
  86.         long l=0, el, i;
  87.         PyObject *e=0;
  88.     
  89.         UNLESS(-1 != (i=PyList_Size(self->data))) return -1;
  90.         while(--i >= 0)
  91.           {
  92.             e=PyList_GetItem(self->data,i);
  93.             UNLESS(-1 != (el=PyObject_Length(e))) return -1;
  94.             l+=el;
  95.           }
  96.         return l;
  97.     }
  98.     
  99.     static PyObject *
  100.     MM_subscript(MMobject *self, PyObject *key){
  101.         long i;
  102.         PyObject *e;
  103.     
  104.         UNLESS(-1 != (i=PyList_Size(self->data))) return NULL;
  105.         while(--i >= 0)
  106.           {
  107.             e=PyList_GetItem(self->data,i);
  108.             if(e=PyObject_GetItem(e,key)) return e;
  109.             PyErr_Clear();
  110.           }
  111.         PyErr_SetObject(PyExc_KeyError,key);
  112.         return NULL;
  113.     }
  114.     
  115.     static PyMappingMethods MM_as_mapping = {
  116.               (inquiry)MM_length,           /*mp_length*/
  117.               (binaryfunc)MM_subscript,             /*mp_subscript*/
  118.               (objobjargproc)NULL,          /*mp_ass_subscript*/
  119.     };
  120.     
  121.     /* -------------------------------------------------------- */
  122.     
  123.     static char MMtype__doc__[] = 
  124.     "MultiMapping -- Combine multiple mapping objects for lookup"
  125.     ;
  126.     
  127.     static PyTypeObject MMtype = {
  128.               PyObject_HEAD_INIT(&PyType_Type)
  129.               0,                            /*ob_size*/
  130.               "MultMapping",                        /*tp_name*/
  131.               sizeof(MMobject),             /*tp_basicsize*/
  132.               0,                            /*tp_itemsize*/
  133.               /* methods */
  134.               (destructor)MM_dealloc,               /*tp_dealloc*/
  135.               (printfunc)0,                 /*tp_print*/
  136.               (getattrfunc)MM_getattr,      /*tp_getattr*/
  137.               (setattrfunc)0,                       /*tp_setattr*/
  138.               (cmpfunc)0,                   /*tp_compare*/
  139.               (reprfunc)0,                  /*tp_repr*/
  140.               0,                            /*tp_as_number*/
  141.               0,                            /*tp_as_sequence*/
  142.               &MM_as_mapping,                       /*tp_as_mapping*/
  143.               (hashfunc)0,                  /*tp_hash*/
  144.               (ternaryfunc)0,                       /*tp_call*/
  145.               (reprfunc)0,                  /*tp_str*/
  146.     
  147.               /* Space for future expansion */
  148.               0L,0L,0L,0L,
  149.               MMtype__doc__ /* Documentation string */
  150.     };
  151.     
  152.     static struct PyMethodDef MultiMapping_methods[] = {
  153.         {"MultiMapping", (PyCFunction)newMMobject, 1,
  154.          "MultiMapping() -- Create a new empty multi-mapping"},
  155.         {NULL,              NULL}           /* sentinel */
  156.     };
  157.     
  158.     void
  159.     initMultiMapping(){
  160.         PyObject *m;
  161.     
  162.         m = Py_InitModule4(
  163.             "MultiMapping", MultiMapping_methods,
  164.               "MultiMapping -- Wrap multiple mapping objects for lookup",
  165.               (PyObject*)NULL,PYTHON_API_VERSION);
  166.     
  167.         if (PyErr_Occurred()) 
  168.            Py_FatalError("can't initialize module MultiMapping");
  169.     }
  170.  
  171.   This module defines an extension type, 'MultiMapping', and exports a
  172.   module function, 'MultiMapping', that creates 'MultiMapping'
  173.   Instances. The type provides two methods, 'push', and 'pop', for
  174.   adding and removing mapping objects to the multi-mapping.
  175.   The type provides mapping behavior, implementing mapping length
  176.   and subscript operators but not mapping a subscript assignment
  177.   operator.
  178.  
  179.   Now consider an extension class implementation of MultiMapping
  180.   objects::
  181.  
  182.     #include "Python.h"
  183.     #include "ExtensionClass.h"
  184.     
  185.     #define UNLESS(E) if(!(E))
  186.     
  187.     typedef struct {
  188.         PyObject_HEAD
  189.         PyObject *data;
  190.     } MMobject;
  191.     
  192.     staticforward PyExtensionClass MMtype;
  193.     
  194.     static PyObject *
  195.     MM_push(self, args)
  196.               MMobject *self;
  197.               PyObject *args;
  198.     {
  199.         PyObject *src;
  200.         UNLESS(PyArg_ParseTuple(args, "O", &src)) return NULL;
  201.         UNLESS(-1 != PyList_Append(self->data,src)) return NULL;
  202.         Py_INCREF(Py_None);
  203.         return Py_None;
  204.     }
  205.     
  206.     static PyObject *
  207.     MM_pop(self, args)
  208.               MMobject *self;
  209.               PyObject *args;
  210.     {
  211.         long l;
  212.         PyObject *r;
  213.         static PyObject *emptyList=0;
  214.     
  215.         UNLESS(emptyList) UNLESS(emptyList=PyList_New(0)) return NULL;
  216.         UNLESS(PyArg_ParseTuple(args, "")) return NULL;
  217.         UNLESS(-1 != (l=PyList_Size(self->data))) return NULL;
  218.         l--;
  219.         UNLESS(r=PySequence_GetItem(self->data,l)) return NULL;
  220.         UNLESS(-1 != PyList_SetSlice(self->data,l,l+1,emptyList)) goto err;
  221.         return r;
  222.     err:
  223.         Py_DECREF(r);
  224.         return NULL;
  225.     }
  226.     
  227.     static PyObject *
  228.     MM__init__(self, args)
  229.            MMobject *self;
  230.            PyObject *args;
  231.     {
  232.         UNLESS(PyArg_ParseTuple(args, "")) return NULL;
  233.         UNLESS(self->data=PyList_New(0)) goto err;
  234.         Py_INCREF(Py_None);
  235.         return Py_None;
  236.     err:
  237.         Py_DECREF(self);
  238.         return NULL;
  239.     }
  240.     
  241.     static struct PyMethodDef MM_methods[] = {
  242.         {"__init__", (PyCFunction)MM__init__, 1,
  243.          "__init__() -- Create a new empty multi-mapping"},
  244.         {"push", (PyCFunction) MM_push, 1,
  245.          "push(mapping_object) -- Add a data source"},
  246.         {"pop",  (PyCFunction) MM_pop,  1,
  247.          "pop() -- Remove and return the last data source added"}, 
  248.         {NULL,              NULL}           /* sentinel */
  249.     };
  250.     
  251.     static void
  252.     MM_dealloc(self)
  253.            MMobject *self;
  254.     {
  255.         Py_XDECREF(self->data);
  256.         PyMem_DEL(self);
  257.     }
  258.     
  259.     static PyObject *
  260.     MM_getattr(self, name)
  261.               MMobject *self;
  262.               char *name;
  263.     {
  264.         return Py_FindMethod(MM_methods, (PyObject *)self, name);
  265.     }
  266.     
  267.     static int
  268.     MM_length(self)
  269.               MMobject *self;
  270.     {
  271.         long l=0, el, i;
  272.         PyObject *e=0;
  273.     
  274.         UNLESS(-1 != (i=PyList_Size(self->data))) return -1;
  275.         while(--i >= 0)
  276.           {
  277.             e=PyList_GetItem(self->data,i);
  278.             UNLESS(-1 != (el=PyObject_Length(e))) return -1;
  279.             l+=el;
  280.           }
  281.         return l;
  282.     }
  283.     
  284.     static PyObject *
  285.     MM_subscript(self, key)
  286.               MMobject *self;
  287.               PyObject *key;
  288.     {
  289.         long i;
  290.         PyObject *e;
  291.     
  292.         UNLESS(-1 != (i=PyList_Size(self->data))) return NULL;
  293.         while(--i >= 0)
  294.           {
  295.             e=PyList_GetItem(self->data,i);
  296.             if(e=PyObject_GetItem(e,key)) return e;
  297.             PyErr_Clear();
  298.           }
  299.         PyErr_SetObject(PyExc_KeyError,key);
  300.         return NULL;
  301.     }
  302.     
  303.     static PyMappingMethods MM_as_mapping = {
  304.               (inquiry)MM_length,           /*mp_length*/
  305.               (binaryfunc)MM_subscript,             /*mp_subscript*/
  306.               (objobjargproc)NULL,          /*mp_ass_subscript*/
  307.     };
  308.     
  309.     /* -------------------------------------------------------- */
  310.     
  311.     static char MMtype__doc__[] = 
  312.     "MultiMapping -- Combine multiple mapping objects for lookup"
  313.     ;
  314.     
  315.     static PyExtensionClass MMtype = {
  316.               PyObject_HEAD_INIT(&PyType_Type)
  317.               0,                            /*ob_size*/
  318.               "MultMapping",                        /*tp_name*/
  319.               sizeof(MMobject),             /*tp_basicsize*/
  320.               0,                            /*tp_itemsize*/
  321.               /* methods */
  322.               (destructor)MM_dealloc,               /*tp_dealloc*/
  323.               (printfunc)0,                 /*tp_print*/
  324.               (getattrfunc)MM_getattr,      /*tp_getattr*/
  325.               (setattrfunc)0,                       /*tp_setattr*/
  326.               (cmpfunc)0,                   /*tp_compare*/
  327.               (reprfunc)0,                  /*tp_repr*/
  328.               0,                            /*tp_as_number*/
  329.               0,                            /*tp_as_sequence*/
  330.               &MM_as_mapping,                       /*tp_as_mapping*/
  331.               (hashfunc)0,                  /*tp_hash*/
  332.               (ternaryfunc)0,                       /*tp_call*/
  333.               (reprfunc)0,                  /*tp_str*/
  334.     
  335.               /* Space for future expansion */
  336.               0L,0L,0L,0L,
  337.               MMtype__doc__, /* Documentation string */
  338.               METHOD_CHAIN(MM_methods)
  339.     };
  340.     
  341.     static struct PyMethodDef MultiMapping_methods[] = {
  342.         {NULL,              NULL}           /* sentinel */
  343.     };
  344.     
  345.     void
  346.     initMultiMapping()
  347.     {
  348.         PyObject *m, *d;
  349.     
  350.         m = Py_InitModule4(
  351.             "MultiMapping", MultiMapping_methods,
  352.             "MultiMapping -- Wrap multiple mapping objects for lookup",
  353.             (PyObject*)NULL,PYTHON_API_VERSION);
  354.         d = PyModule_GetDict(m);
  355.         PyExtensionClass_Export(d,"MultiMapping",MMtype);
  356.     
  357.         if (PyErr_Occurred()) 
  358.            Py_FatalError("can't initialize module MultiMapping");
  359.     }
  360.  
  361.   This version includes 'ExtensionClass.h'.  The two declarations of
  362.   'MMtype' have been changed from 'PyTypeObject' to 'PyExtensionClass'.
  363.   The 'METHOD_CHAIN' macro has been used to add methods to the end of
  364.   the definition for 'MMtype'.  The module function, newMMobject has
  365.   been replaced by the 'MMtype' method, 'MM__init__'.  Note that this
  366.   method does not create or return a new object.  Finally, the lines::
  367.  
  368.     d = PyModule_GetDict(m);
  369.     PyExtensionClass_Export(d,"MultiMapping",MMtype);
  370.  
  371.   Have been added to both initialize the extension class and to export
  372.   it in the module dictionary.
  373.  
  374.   To use this module, compile, link, and import it as with any other
  375.   extension module.  The following python code illustrates the
  376.   module's use::
  377.  
  378.     from MultiMapping import MultiMapping
  379.     m=MultiMapping()
  380.     m.push({'spam':1, 'eggs':2})
  381.     m.push({'spam':3, 'ham':4})
  382.  
  383.     m['spam'] # returns 3
  384.     m['ham']  # returns 4
  385.     m['foo']  # raises a key error
  386.  
  387.   Creating the 'MultiMapping' object took three steps, one to create
  388.   an empty 'MultiMapping', and two to add mapping objects to it.  We
  389.   might wish to simplify the process of creating MultiMapping
  390.   objects by providing a constructor that takes source mapping
  391.   objects as parameters.  We can do this by sub-classing MultiMapping
  392.   in Python::
  393.  
  394.     from MultiMapping import MultiMapping
  395.     class ExtendedMultiMapping(MultiMapping):
  396.         def __init__(self,*data):
  397.           MultiMapping.__init__(self)
  398.           for d in data: self.push(d)
  399.  
  400.     m=ExtendedMultiMapping({'spam':1, 'eggs':2}, {'spam':3, 'ham':4})
  401.  
  402.     m['spam'] # returns 3
  403.     m['ham']  # returns 4
  404.     m['foo']  # raises a key error
  405.  
  406.   Note that the source file included in the ExtensionClass
  407.   distribution has numerous enhancements beyond the version shown in
  408.   this document.
  409.